package com.runcode.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import lombok.extern.slf4j.Slf4j;
/**
 * 服务消费者
 * @author w
 */
@Slf4j
@RestController
public class CustomerController {

	@Autowired
	private RestTemplate restTemplate ;
	
	@RequestMapping(value = "/hello")
	public String hello(String name) {
		return "hello Customer: \t" + name;
	}
	
	/**
	 * 调用服务提供者，直接传参
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/hello/customer")
	public String helloCustomer(String name) {
		log.info("/hello/customer , param {}" , name);
		String res = restTemplate.getForObject("http://cloud-producer/hello?name={name}", String.class, name);
		return res ;
	}
	
	/**
	 * 调用服务提供者，直接传参
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/customer")
	public String customer(String name) {
		String res = restTemplate.getForObject("http://cloud-producer/producer?name"+name, String.class);
		return res ;
	}
	
	/**
	 * 调用服务提供者，url传参
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/customer/{name}")
	public String customers(@PathVariable String name) {
		String res = restTemplate.getForObject("http://cloud-producer/producer/"+ name, String.class);
		return res ;
	}
}
